www.gusucode.com > VC++ 宾馆管理系统(MSSQL) > VC++ 宾馆管理系统(MSSQL)/gusucode/Code/RoomDLG.cpp

    //Download by http://www.NewXing.com
// RoomDLG.cpp : implementation file
//

#include "stdafx.h"
#include "hotel_mis.h"
#include "RoomDLG.h"
#include "Hotel_MISView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRoomDLG dialog


CRoomDLG::CRoomDLG(CWnd* pParent /*=NULL*/)
	: CDialog(CRoomDLG::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRoomDLG)
	m_sMemo = _T("");
	m_sNo = _T("");
	m_sPosition = _T("");
	m_sPrice = _T("");
	m_sType = _T("");
	//}}AFX_DATA_INIT
}

void CRoomDLG::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRoomDLG)
	DDX_Text(pDX, IDD_ROOM_MEMO, m_sMemo);
	DDX_Text(pDX, IDD_ROOM_NO, m_sNo);
	DDX_Text(pDX, IDD_ROOM_POSITION, m_sPosition);
	DDX_Text(pDX, IDD_ROOM_PRICE, m_sPrice);
	DDX_CBString(pDX, IDD_ROOM_TYPE, m_sType);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRoomDLG, CDialog)
	//{{AFX_MSG_MAP(CRoomDLG)
	ON_CBN_SELCHANGE(IDD_ROOM_TYPE, OnRoomTypeChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRoomDLG message handlers

void CRoomDLG::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(true);
	
	m_sNo.TrimRight(" ");
    m_sPosition.TrimRight(" ");
	m_sPrice.TrimRight(" ");
	m_sMemo.TrimRight(" ");
	
	// Make sure all needed info is available
	CString sWarning="";
	if ( ""==m_sNo ) sWarning=_T("客房编号");
    else if ( ""==m_sPosition ) sWarning=_T("客房位置");
	else if ( ""==m_sPrice ) sWarning=_T("客房价格");
	
	if ( ""!=sWarning ) 
	{
	   sWarning += _T("不能为空");
	   AfxMessageBox(sWarning, MB_ICONEXCLAMATION);
	   return;
	}

	// Make sure the Number info is valid
	float fPrice=atof(m_sPrice);
	if ( 0==fPrice ) 
	{
		AfxMessageBox(_T("客房单价:请输入非零数字"), MB_ICONEXCLAMATION);
	    return;
	}
	m_sPrice.Format("%.2f", fPrice);
	
	_variant_t strQuery;		
	if (m_bAppend)
	{
		// Judge Room No is Unique
	    strQuery = "select * from rooms where roomNO='"+m_sNo+"'";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	    int iCount = theApp.m_pADOSet->GetRecordCount();
	    if ( 0!=iCount )
		{
	       AfxMessageBox(_T("已经存在此客房编号的记录!"), MB_ICONEXCLAMATION);
	       return;
		}
	}
    
	if (m_bAppend)// Append Record
	{
	    strQuery = "insert rooms (roomNo, roomtype, roomposition, roomprice, roommemo) \
	         	    values ('"+m_sNo+"', '"+m_sType+"',  '"+m_sPosition+"', "+m_sPrice+", '"+m_sMemo+"')";
	    if ( theApp.ADOExecute(theApp.m_pADOSet, strQuery) ) 
		{
			AfxMessageBox(_T("添加记录成功!"), MB_ICONINFORMATION);
	        
			// Clear all input
            m_sNo=m_sType=m_sPosition=m_sPrice=m_sMemo="";
            ((CComboBox*)GetDlgItem(IDD_ROOM_TYPE))->SetCurSel(0);
		    UpdateData(false);
		}
	    else AfxMessageBox(_T("添加记录失败!"), MB_ICONEXCLAMATION);
	}
    else
	{
		strQuery = "Update rooms set roomtype='"+m_sType+"', roomposition='"+m_sPosition+"', roomprice="+m_sPrice+", roommemo='"+m_sMemo+"' \
			        where roomNo='"+m_sNo+"'";
	    if ( theApp.ADOExecute(theApp.m_pADOSet, strQuery) ) AfxMessageBox(_T("修改记录成功!"), MB_ICONINFORMATION);
	    else AfxMessageBox(_T("修改记录失败!"), MB_ICONEXCLAMATION);
	}
	
	// Refresh Room List
	strQuery = "select * from rooms";
	CHotel_MISView* p = (CHotel_MISView*)(((CMainFrame*)AfxGetMainWnd())->GetActiveView());
	p->RefreshRoom(strQuery);
	
	if (!m_bAppend) CDialog::OnOK();
}

BOOL CRoomDLG::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	
	// Set Edit Text limit
	((CEdit*)GetDlgItem(IDD_ROOM_NO))->SetLimitText(10);
	((CEdit*)GetDlgItem(IDD_ROOM_POSITION))->SetLimitText(20);
	((CEdit*)GetDlgItem(IDD_ROOM_PRICE))->SetLimitText(8);
	
	if (m_bAppend)
	{		
	    // Update Dialog Caption
		SetWindowText(_T("添加客房信息"));
		
		// Init Combobox
		_variant_t strQuery, Holder;	
		strQuery = "select * from roomtype";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	    int iCount = theApp.m_pADOSet->GetRecordCount();
		theApp.m_pADOSet->MoveFirst();
		for (int i=0; i<iCount; i++)
		{
		    Holder = theApp.m_pADOSet->GetCollect("typename");
	        ((CComboBox*)GetDlgItem(IDD_ROOM_TYPE))->InsertString(i, Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder);
		    // Get price to display
			if ( 0==i )
			{
				Holder = theApp.m_pADOSet->GetCollect("price");
				m_sPrice.Format("%.2f", Holder.dblVal);
			}
			
			theApp.m_pADOSet->MoveNext();
		}
	    ((CComboBox*)GetDlgItem(IDD_ROOM_TYPE))->SetCurSel(0);
	}
	else
	{
		// Update Dialog Caption
		SetWindowText(_T("修改客房信息"));
		
		// Disable room no edit
		GetDlgItem(IDD_ROOM_NO)->EnableWindow(false);
		
		// Init Combobox
		_variant_t strQuery, Holder;	
		CString sType;
		int iSel=0;
		
		strQuery = "select typename from roomtype";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	    int iCount = theApp.m_pADOSet->GetRecordCount();
		theApp.m_pADOSet->MoveFirst();
		for (int i=0; i<iCount; i++)
		{
		    Holder = theApp.m_pADOSet->GetCollect("typename");
	        sType = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			((CComboBox*)GetDlgItem(IDD_ROOM_TYPE))->InsertString(i, sType);
		    if ( sType==m_sType ) iSel=i;
			theApp.m_pADOSet->MoveNext();
		}
	    
		((CComboBox*)GetDlgItem(IDD_ROOM_TYPE))->SetCurSel(iSel);
	}
	
	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CRoomDLG::OnRoomTypeChange() 
{
	// TODO: Add your control notification handler code here
	
	UpdateData(true);
	
	// Get Room type
	int iIndex = ((CComboBox*)GetDlgItem(IDD_ROOM_TYPE))->GetCurSel();
	CString sType;
    ((CComboBox*)GetDlgItem(IDD_ROOM_TYPE))->GetLBText(iIndex, sType);
	
	_variant_t strQuery, Holder;
    strQuery = "select price from roomtype where typename='"+sType+"'";
	theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	int iCount = theApp.m_pADOSet->GetRecordCount();
	if ( 0==iCount )
    {
		AfxMessageBox(_T("读取客房价格错误!"), MB_ICONEXCLAMATION);
		return;
	}
    
	// Get Price and Update Dialog 
	Holder = theApp.m_pADOSet->GetCollect("price");
	m_sPrice.Format("%.2f", Holder.dblVal);
    UpdateData(false);
}